package com.tjcd.project.library_base.base.bean;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * @Author jiangKunKun
 * @CreateDate 2021/7/6
 * @Description
 * @SpittingGrooves
 */
public class BaseJsonRequestBean extends JSONObject {

    /**
     * 去空
     *
     * @param name
     * @param value
     * @return
     */
    public BaseJsonRequestBean add(String name, Object value) {
        try {
            if ("".equals(name)) {
                return this;
            }
            if (null == value) {
                this.remove(name);
                return this;
            }
            this.put(name, value);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return this;
    }

    /**
     * 去空
     *
     * @param name
     * @param value
     * @return
     */
    public BaseJsonRequestBean addObjList(String name, List<?> value) {
        try {
            if ("".equals(name)) {
                return this;
            }
            if (null == value) {
                this.remove(name);
                return this;
            }
            JSONArray array = new JSONArray();
            array.addAll(value);
            this.put(name, array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return this;
    }
    /**
     * 去空
     *
     * @param name
     * @param value
     * @return
     */
    public BaseJsonRequestBean addLongList(String name, List<Long> value) {
        try {
            if ("".equals(name)) {
                return this;
            }
            if (null == value) {
                this.remove(name);
                return this;
            }
            JSONArray array = new JSONArray();
            array.addAll(value);
            this.put(name, array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return this;
    }

    /**
     * 去空
     *
     * @param name
     * @param value
     * @return
     */
    public BaseJsonRequestBean addIntgerList(String name, List<Integer> value) {
        try {
            if ("".equals(name)) {
                return this;
            }
            if (null == value) {
                this.remove(name);
                return this;
            }
            JSONArray array = new JSONArray();
            array.addAll(value);
            this.put(name, array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return this;
    }

    /**
     * 去空
     *
     * @param name
     * @param value
     * @return
     */
    public BaseJsonRequestBean addStrList(String name, List<String> value) {
        try {
            if ("".equals(name)) {
                return this;
            }
            if (null == value) {
                this.remove(name);
                return this;
            }
            JSONArray array = new JSONArray();
            array.addAll(value);
            this.put(name, array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return this;
    }

    /**
     * 去空
     *
     * @param name
     * @param value
     * @return
     */
    public BaseJsonRequestBean add(String name, String value) {
        try {
            if ("".equals(name)) {
                return this;
            }
            if (null == value) {
                this.remove(name);
                return this;
            }
            this.put(name, value);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return this;
    }
}
